package edu.umn.msse.farside.FourH.web;

import com.dreamlizard.miles.beans.TimestampPropertyEditor;
import edu.umn.msse.farside.FourH.domain.Event;
import edu.umn.msse.farside.FourH.domain.Position;
import edu.umn.msse.farside.FourH.domain.Volunteer;
import edu.umn.msse.farside.FourH.services.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

/**
 * Created by IntelliJ IDEA.
 * User: corcutt
 * Date: Mar 22, 2009
 * Time: 4:54:53 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/editPosition")
public class EditPositionController
{
    private EventService eventService;

    @Autowired
    public void setEventService(EventService eventService) {
        this.eventService = eventService;
    }

    @InitBinder
    // bind timestamp type to a propertyeditor to properly create
    public void setAllowedFields(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Timestamp.class, new TimestampPropertyEditor());
    }

    @RequestMapping(method = RequestMethod.GET)
    public String editPositionForm(@RequestParam(required=true) Long positionID, @RequestParam(required=true) Long eventID, Model model, HttpSession session) {
        // positionID comes in on the request as a hidden parameter
        Volunteer volunteer = (Volunteer)session.getAttribute("volunteer");
        Event event = eventService.getEvent(eventID);
        if (event.getAdministrator().getId().equals(volunteer.getId()))
        {
            model.addAttribute("event", eventService.getEvent(eventID));
            model.addAttribute("position", eventService.getPosition(positionID));

            return "editPosition";
        }
        else
        { // not the administrator .. someone is trying to sneak in .. redirect
            return "redirect:futureEvents";
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public String editPositionForm(@RequestParam(required = true) Long eventID, @ModelAttribute(value = "position") Position position, BindingResult result, Model model)
    {
        // any validation checking
        Event event = eventService.getEvent(eventID);
        model.addAttribute("event", event);
        new PositionValidator().validate(position, result, event);

        Position updatedPosition = eventService.getPosition(position.getId());

        if (result.hasErrors())
        {
            position.setVolunteers(updatedPosition.getVolunteers());
            return "editPosition";
        }
        else
        {
            updatedPosition = updatedPosition.updatePosition(position);
            // Event thisEvent = eventService.getEvent(event.getId());
            updatedPosition = eventService.updatePosition(updatedPosition);

            model.addAttribute("position", updatedPosition);

            return "redirect:editEvent?eventID=" + eventID;
        }
    }



}
